using Quartz;
using Quartz.Simpl;
using Zhao.Arena.Utility.AuthorizationExt;
using Zhao.Arena.Utility.InitDatabaseExt;
using Zhao.Arena.Utility.QuartZJob;
using Zhao.Arena.Utility.QuartzExt;
using Zhao.Arena.Utility.RegisterExt;
using Zhao.Arena.Utility.SwaggerExt;
using Zhao.Common.AliyunUtil.Model;
using Zhao.Common.Helper;
using Zhao.Common.MapConfig;
using Newtonsoft.Json.Converters;
using Zhao.Service;

namespace Zhao.Arena
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            //配置跨域
            builder.CorsDomainsPolicy();
            //初始化SqlSugar--注册到IOC容器
            builder.InitSqlSugar();

            builder.Services.AddControllers();

            //IMemoryCache容器注册
            builder.Services.AddMemoryCache();

            //直接调用扩展方法 配置swagger
            builder.AddSwaggerExt();
            //配置阿里云存储OSS Api配置
            builder.Services.Configure<AliyunOSSSetting>(builder.Configuration.GetSection("AliyunOSSSetting"));

            //日志等级注册
            builder.Services.Configure<LogConfigModel>(builder.Configuration.GetSection("LogConfig"));

            //服务注入集合调用
            builder.RegistService();
            builder.RegistControllers();

            //配置节假日json文件的地址
            builder.Services.Configure<HolidaySettings>(builder.Configuration.GetSection("HolidaySettings"));

            #region 定时任务设置注册
            // 使用扩展方法注册所有Quartz定时任务
            builder.AddAllQuartzJobs();
            #endregion

            #region ASE密钥获取及注册
            string base64Key = builder.Configuration["Encryption:AesKeyBase64"];
            if (string.IsNullOrWhiteSpace(base64Key))
                throw new InvalidOperationException("AES 密钥不能为空");

            byte[] aesKey = Convert.FromBase64String(base64Key); // 转化为 byte[] 给予 AesGcmHelper

            //注册加密解密服务
            builder.Services.AddSingleton(new AesGcmHelper(aesKey));
            #endregion

            #region automapper
            builder.Services.AddAutoMapper(typeof(AutoMapperConfigs));
            #endregion

            #region 初始化菜单
            //builder.InitDatabase();
            #endregion

            builder.AuthorizationExt(); //添加Jwt认证授权服务

            var app = builder.Build();

            #region 项目加载的时候预加载地区数据
            // 从容器中获取 AreaCacheManager 并预加载
            using (var scope = app.Services.CreateScope())
            {
                var cacheManager = scope.ServiceProvider.GetRequiredService<AreaCacheManager>();
                await cacheManager.InitializeCache(); // 预加载省市区数据到缓存
            }
            #endregion

            //配置跨域策略生效
            app.UseCorsDomainsPolicy();

            // Configure the HTTP request pipeline.
            //if (app.Environment.IsDevelopment())
            //{
            //直接调用扩展方法 注册swagger服务
            app.UserSgwaagerExt();
            //}

            app.UseHttpsRedirection();

            app.UseAuthentication();//认证
            app.UseAuthorization();//授权

            app.MapControllers();

            app.Run();
        }
    }
}
